package com.jftone.bh.app.action;

import com.jftone.bh.app.config.SysConst;
import com.jftone.bh.app.data.StaticInfoQry;
import com.jftone.bh.app.model.NtbBase;
import com.jftone.bh.app.model.NtbNewslist;
import com.jftone.bh.app.service.NewsService;
import com.jftone.bh.app.util.UploadFile;
import org.apache.log4j.Logger;
import org.jftone.annotation.Autowired;
import org.jftone.annotation.Controller;
import org.jftone.exception.ActionException;
import org.jftone.exception.ComponentException;
import org.jftone.util.DataMap;
import org.jftone.util.DateUtil;
import org.jftone.util.IData;
import org.jftone.util.StringUtil;

import javax.servlet.http.HttpServletRequest;
import java.util.List;


@Controller(mapping="/newsAction")
public class NewsAction extends BaseAction {

	private Logger logger = Logger.getLogger(NewsAction.class);
	
	@Autowired
	private NewsService service;



	public void query() throws ActionException {
		IData<String, Object> data = getData();
		String newsTitle = data.getString("newsTitle", "");
		String ctype = data.getString("ctype", "");
		this.putRenderInfo("ctype", ctype);
		this.putRenderInfo("newsTitle", newsTitle);
		try {
			//设置功能按钮
			setMenuFuncDerictive();
			//设置分页组件
			setPageDerictive();
			putParam("ctype", ctype);
			putParam("newsTitle", newsTitle);
			putParam(SysConst.QUERY_PARAMS, getParam());
			this.putRenderInfo("ctypeList", StaticInfoQry.getParams(SysConst.NEWS_CTYPE));
			List<IData<String, Object>> list = service.queryPageList(data, getPage());
			this.putRenderInfo("list", list);
			this.render("news/news_list.html");
		} catch (Exception e) {
			logger.error("查询资讯列表错误" + e.getMessage(), e);
			throw new ActionException("查询资讯列表错误" + e.getMessage(), e);
		}
	}

	public void add() throws ActionException {
		NtbNewslist news = new NtbNewslist();
		news.setIsTop(0);
		this.putRenderInfo("news",news);
		this.putRenderInfo("ctypeList", StaticInfoQry.getParams(SysConst.NEWS_CTYPE));
		this.render("news/news_form.html");
	}

	public void edit() throws ActionException {
		IData<String, Object> data = getData();
		String dataId = data.getString("dataId");
		if(StringUtil.isBlank(dataId) || !StringUtil.isNumber(dataId)){
			this.alertMsg("参数传值错误");
			return;
		}
		try {
			IData<String, Object> inData = new DataMap<String, Object>();
			inData.put("id", Integer.parseInt(dataId));
			NtbNewslist news = service.getNtbNewslist(inData);
			this.putRenderInfo("news", news);
			this.putRenderInfo("ctypeList", StaticInfoQry.getParams(SysConst.NEWS_CTYPE));
			this.render("news/news_form.html");
		} catch (Exception e) {
			logger.error("编辑资讯数据错误"+e.getMessage(), e);
			throw new ActionException("编辑资讯信息错误"+e.getMessage(), e);
		}
	}

	public void save() throws ActionException{
		boolean updateFlag = false;
		IData<String, Object> data = getData();
		String newsId = data.getString("newsId");
		if(!StringUtil.isBlank(newsId) && StringUtil.isNumber(newsId)){
			updateFlag = true;
		}
		try {
			int resCode = service.saveModel(data, updateFlag);
			this.showMsg("资讯信息"+(updateFlag? "保存" : "新增")+"成功", "?method=query");
		} catch (Exception e) {
			logger.error("保存资讯信息错误"+e.getMessage(), e);
			throw new ActionException("保存资讯信息错误"+e.getMessage(), e);
		}
	}

	public void delete() throws ActionException{
		IData<String, Object> data = getData();
		String dataId = data.getString("dataId", "");
		if(StringUtil.isBlank(dataId) || !StringUtil.isNumber(dataId)){
			this.alertMsg("参数传值错误");
			return;
		}
		try {
			int resultCode = service.delete(Integer.parseInt(dataId));
			if(resultCode == 1){
				this.alertMsg("数据错误，请刷新后重试");
				return;
			}
			this.showMsg("删除成功", "?method=query");
		} catch (Exception e) {
			logger.error("删除数据错误"+e.getMessage(), e);
			throw new ActionException("删除数据错误"+e.getMessage(), e);
		}

	}








	public void uploadFile() throws ActionException {
		IData<String, Object> data = getData();
		String imgSrc = data.getString("imgSrc", "");
		Long imgId = data.getLong("imgId", 0L);
		try {
			this.putRenderInfo("imgSize", 50000000);
			putRenderInfo("resultCode", data.getString("resultCode", "-1"));
			putRenderInfo("imgSrc", imgSrc);
			putRenderInfo("imgId", imgId);
			this.render("news/news_upload.html");
		} catch (Exception e) {
			logger.error("上传文件页面加载错误" + e.getMessage(), e);
			throw new ActionException("上传文件页面加载错误" + e.getMessage(), e);
		}
	}

	public void upload() throws Exception {
		Long maxSize = 50000000L;
		String redirectUrl ="newsAction.do?method=uploadFile";
		this.upload(StringUtil.getRandomCode(12, StringUtil.CHARNUM_MODE), UploadFile.FileDir.NEWS, maxSize, 10 * 1024, redirectUrl);
	}
}
